package com.example.concurrentprogramming8016.future.futuretask;

import java.sql.Time;
import java.util.concurrent.*;

/**
 * @author liyanhao
 * @Date 2023-01-08 15:31
 */
public class FutureTaskDemo2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> ft1 = new FutureTask<>(new T1Task());
        FutureTask<String> ft2 = new FutureTask<>(new T2Task());
        FutureTask<String> ft3 = new FutureTask<>(new T3Task());
        FutureTask<String> ft4 = new FutureTask<>(new T4Task());
        FutureTask<String> ft5 = new FutureTask<>(new T5Task());

        //构建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        threadPool.submit(ft1);
        threadPool.submit(ft2);
        threadPool.submit(ft3);
        threadPool.submit(ft4);
        threadPool.submit(ft5);

        //获取执行结果 调用get方法会阻塞线程
        System.out.println(ft1.get());
        System.out.println(ft2.get());
        System.out.println(ft3.get());
        System.out.println(ft4.get());
        System.out.println(ft5.get());
    }

    static class T1Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("T1:查询商品的基本信息");
            TimeUnit.SECONDS.sleep(1);
            return "商品信息查询成功";
        }
    }

    static class T2Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("T2:查询商品价格信息");
            TimeUnit.SECONDS.sleep(1);
            return "商品价格信息查询成功";
        }
    }

    static class T3Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("T3:查询商品的库存信息");
            TimeUnit.SECONDS.sleep(1);
            return "商品库存查询成功";
        }
    }

    static class T4Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("T4:查询商品图片");
            TimeUnit.SECONDS.sleep(1);
            return "商品图片查询成功";
        }
    }

    static class T5Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("T5:查询商品的销售信息");
            TimeUnit.SECONDS.sleep(1);
            return "商品的销售信息查询成功";
        }

    }
}
